"""
@Time ： 2021/11/11 15:45
@Auth ： ChenZhangKun
"""
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import Consistant


class Login:
    def __init__(self, chormePath, url):
        self.chormePath = chormePath
        self.url = url

    def openUrl(self):
        """
        打开网页
        :return:
        """
        self.webDriver = webdriver.Chrome(self.chormePath)
        self.webDriver.implicitly_wait(Consistant.TEN)
        self.webDriver.get(self.url)

    def second_share_code(self):
        """
        卖方再次分享二维码
        :return:
        """
        time.sleep(2)
        # 切换到办件查询
        self.select_main_view(Consistant.BAN_JIAN_CHA_XUN)
        # 点击第一个详情
        self.click_cao_zuo(1, "详情")
        time.sleep(3)
        # 分享二维码
        elements=self.webDriver.find_elements(By.CLASS_NAME,"v-btn__content")
        elements[6].click()
        time.sleep(3)
        # 截图
        self.screen_cut('code2.png')

    def test_login(self, username, password):
        # 登录
        self.login(username, password)
        # 点击首页
        self.click_main()
        # 切换单位按钮
        self.change_danwei_button()
        # 切换角色
        self.change_person_role(Consistant.SECOND)
        # 切换到期房买卖
        self.select_main_view(Consistant.QIFANG_SELL)
        #  点击楼盘表
        self.select_xiaoqu(Consistant.FISRT_LOUPAN)
        # 选择房屋号 201号房
        self.select_house_num(Consistant.SECOND)
        # 点击申请
        self.click_apply()
        # 是否有业务
        # flag = self.check_isExist("class name", "btn")
        # elements = self.webDriver.find_elements(By.CLASS_NAME, "btn")
        # f = False
        # for el in elements:
        #     if el.text == "知道了":
        #         f = True
        #         break
        # # 有业务
        # if flag and f:
        #     print("进入有业务的了返回首页了")
        #     # 点击知道了
        #     self.click_know()
        #     # 返回首页
        #     self.return_main()
        #     # 办件查询
        #     self.select_main_view(Consistant.BAN_JIAN_CHA_XUN)
        #     # 点击第一个详情
        #     self.click_cao_zuo(3, '详情')
        # 输入成交价格
        time.sleep(2)
        print("输入成交价格了")
        self.input_price_and_shoufu(1000000, 300000)
        # 切换不动产标签
        time.sleep(3)
        print("切换不动产标签")
        self.change_tag("不动产")
        time.sleep(1)
        # 切换到买房物权
        print("买房物权")
        self.change_tag("卖方物权")
        time.sleep(1)
        # 切换发=到卖方
        print("切换到卖方")
        self.change_tag("卖方")
        # 填写输入框内容
        print("输入微信号")
        self.find_textarea("请输入微信号", "kd")
        time.sleep(1)
        # 切换到卖方·1合同
        print("切换到买卖合同")
        self.change_tag("买卖合同")
        time.sleep(2)
        # 滚动鼠标
        self.scroll_mouse()
        time.sleep(1)
        # 切换到询问笔录
        print("切换到询问笔录")
        self.change_tag("询问笔录(转出方)")
        time.sleep(3)
        # 上传资料
        print("切换到上传资料")
        self.change_tag("上传资料")
        time.sleep(1)
        self.upload_file(8, r'D:\陈彰坤.jpg')
        time.sleep(2)
        self.share_code()
        # 截屏
        print("截屏")
        time.sleep(3)
        self.screen_cut('code1.png')
    def sign(self):
        """
        签名
        :return:
        """
        time.sleep(3)
        element = self.webDriver.find_element(By.CLASS_NAME, "canvasWrap")
        ac = ActionChains(self.webDriver)
        # 长按左键
        ac.click_and_hold(element).move_by_offset(20, 0).perform()
        time.sleep(2)
        # 释放
        ac.release(element)
        # 选择确认
        elements = self.webDriver.find_elements(By.TAG_NAME, "span")
        for el in elements:
            if el.text == '确认':
                el.click()
        # 点击知道了
        self.click_know()
    def saomiao_code(self, path):
        """
        扫描
        :param path:
        :return:
        """
        time.sleep(3)
        element = self.webDriver.find_element(By.CSS_SELECTOR, "input[type='file']")
        time.sleep(2)
        element.send_keys(path)

    def submit_manage_dept(self):
        """
        提交管理部门审核
        :return:
        """
        elements=self.webDriver.find_elements(By.TAG_NAME,"button")
        time.sleep(1)
        for el in elements:
            if el.text=="提交管理部门审核":
                el.click()
                break
    def cicle_change_tag(self,tags):
        """
        虚幻切换标签
        :param tags:
        :return:
        """
        for tag in tags:
            time.sleep(2)
            self.change_tag(tag)
    def share_code(self):
        """
        点击分享二维码
        :return:
        """
        elemnts = self.webDriver.find_elements(By.TAG_NAME, "span")
        for el in elemnts:
            if el.text == "分享二维码给买方":
                el.click()
                break

    def input_price_and_shoufu(self, price, shoufu):
        """
        输入价格和首付
        :param price:
        :param shoufu:
        :return:
        """
        time.sleep(1)
        elements = self.webDriver.find_elements(By.CSS_SELECTOR, "textarea[placeholder='请输入成交价格']")
        time.sleep(1)
        elements[Consistant.ZERO].clear()
        time.sleep(1)
        elements[Consistant.ZERO].send_keys(price)
        # 看是否有首付 由于检查异常会很慢，这里暂且注释掉
        # flag = self.check_isExist("css selector", "textarea[placeholder='请输入首付款']")
        # if flag:
        #     print("有首付款")
        #     elements = self.webDriver.find_elements(By.CSS_SELECTOR, "textarea[placeholder='请输入首付款']")
        #     elements[Consistant.ZERO].send_keys(shoufu)

    def click_cao_zuo(self, num, name):
        """
        第几条业务的第几个操作
        :param num:
        :param name:
        :return:
        """
        time.sleep(2)
        spans = self.webDriver.find_elements(By.TAG_NAME, "span")
        i = 0
        for span in spans:
            if span.text == name:
                i += 1
                if i == num:
                    span.click()
                    break

    def return_main(self):
        print("返回首页")
        time.sleep(1)
        elements = self.webDriver.find_elements(By.TAG_NAME, "button")
        elements[Consistant.ZERO].click()
        time.sleep(1)
        elements = self.webDriver.find_elements(By.TAG_NAME, "a")
        elements[Consistant.ZERO].click()

    def click_know(self):
        """
        点击知道了
        :return:
        """
        time.sleep(2)
        know = self.webDriver.find_elements(By.CLASS_NAME, "btn")
        if know is not None:
            print("点击知道了")
            know[0].click()

    def click_apply(self):
        """
        点击申请
        :return:
        """
        shengqing_elements = self.webDriver.find_elements(By.TAG_NAME, "span")
        print("点击申请")
        i = 0
        for span in shengqing_elements:
            if span.text == '申请':
                span.click()

    def select_house_num(self, house_num):
        """
        学则房屋号码
        :param house_num:
        :return:
        """
        print("选择房屋号码")
        time.sleep(2)
        num_elements = self.webDriver.find_elements(By.CLASS_NAME, "spanZi")
        # 选择103
        num_elements[house_num].click()
        time.sleep(2)

    def select_xiaoqu(self, num):
        """
        点击楼盘
        :param num: 楼盘编号
        :return:
        """
        print("点击楼盘")
        loupan_elements = self.webDriver.find_elements(By.TAG_NAME, "button")
        print(len(loupan_elements))
        loupan_elements[num].click()

    def select_main_view(self, viewNum):
        """
        切换到主菜单的按钮
        :param viewNum:
        :return:
        """
        time.sleep(Consistant.THREE)
        main_view = self.webDriver.find_elements(By.CSS_SELECTOR, "div[align=center]")
        main_view[viewNum].click()
        time.sleep(3)

    def change_person_role(self, role_num):
        """
        切换角色
        :param role_num:
        :return:
        """
        role_elements = self.webDriver.find_elements(By.CSS_SELECTOR, "div[role=listitem]")
        print(len(role_elements))
        role_elements[role_num].click()

    def change_danwei_button(self):
        """
        切换单位按钮
        :return:
        """
        a_elements = self.webDriver.find_elements(By.TAG_NAME, "a")
        a_elements[Consistant.THREE].click()
        time.sleep(Consistant.CHANGE_DANWEI_BUTTON)

    def click_main(self):
        """
        点击首页
        :return:
        """
        time.sleep(Consistant.THREE)
        elements = self.webDriver.find_elements(By.XPATH, "//button")
        elements[Consistant.CLICK_MAIN].click()
        self.webDriver.implicitly_wait(100)

    def login(self, username, password):
        """
        登录
        :param username:
        :param password:
        :return:
        """
        usernameEle = self.webDriver.find_element(By.CSS_SELECTOR, "input[name=login]")
        print(usernameEle)
        usernameEle.send_keys(username)
        passwordEle = self.webDriver.find_element(By.CSS_SELECTOR, "input[name=password]")
        print(passwordEle)
        passwordEle.send_keys(password)
        btnsEle = self.webDriver.find_elements(By.CLASS_NAME, "v-btn__content")
        for btn in btnsEle:
            print(btn.text)
            if btn.text == "登录":
                btn.click()
                break
        flag = self.check_isExist("class name", "btn")
        if flag:
            noEle = self.webDriver.find_element(By.CLASS_NAME, "btn")
            print(noEle.text)
            noEle.click()
        # 选择是
        # if username == '15723315609':
        #     noEle = self.webDriver.find_element(By.CLASS_NAME, "btn")
        #     print(noEle.text)
        #     noEle.click()

    def check_isExist(self, attach, content):
        """
        检查元素是否存在
        :param attach:
        :param content:
        :return:
        """
        driver = self.webDriver
        flag = True
        try:
            driver.find_element(attach, content)
            return flag
        except Exception as e:
            flag = False
            return flag

    def scroll_mouse(self):
        """
        滚动鼠标
        :return:
        """
        print("滚动鼠标")
        js = "window.scrollTo(0, document.body.scrollHeight)"
        self.webDriver.execute_script(js)
        time.sleep(5)
        # 会导顶部
        js = "window.scrollTo(0,0)"
        self.webDriver.execute_script(js)
        time.sleep(5)

    def screen_cut(self, path):
        """
        截屏
        :param path:
        :return:
        """
        self.webDriver.get_screenshot_as_file(path)

    def upload_file(self, num, path):
        """
        上传图片
        :param num:
        :param path:
        :return:
        """
        elements = self.webDriver.find_elements(By.TAG_NAME, "img")
        elements[num].click()
        time.sleep(3)
        print("相册上传")
        elements = self.webDriver.find_elements(By.CSS_SELECTOR, "div[role=listitem]>input")
        #  elements=self.webDriver.find_elements(By.XPATH,"//input")
        time.sleep(2)
        elements[0].send_keys(path)

    def find_textarea(self, txt, value):
        """
        输入内容
        :param txt:
        :param value:
        :return:
        """
        elements = self.webDriver.find_elements(By.CSS_SELECTOR, "textarea[placeholder=" + txt + "]")
        elements[Consistant.ZERO].clear()
        elements[Consistant.ZERO].send_keys(value)

    def change_tag(self, tag_name):
        """
        切换标签
        :param tag_name:
        :return:
        """
        spans = self.webDriver.find_elements(By.TAG_NAME, "span")
        for span in spans:
            if span.text == tag_name:
                span.click()
                break

    def change_role(self):
        # elements = self.webDriver.find_elements(By.XPATH, "//button")
        # self.webDriver.implicitly_wait(100)
        # # 拿到第二个
        # element = elements[1]
        # element.click()
        # element.click()
        # # 切换单位
        # self.webDriver.implicitly_wait(100)
        # a_elements = self.webDriver.find_elements(By.TAG_NAME, "a")
        # a_elements[3].click
        pass


if __name__ == '__main__':
    webDriver = webdriver.Chrome(r'D:\sofwares\chrom\chromedriver.exe')
    # 最大等待十秒钟
    webDriver.implicitly_wait(10)

    webDriver.get("http://yancheng.bit-service.com:8082/ii/#/login")

    usernameEle = webDriver.find_element(By.CSS_SELECTOR, "input[name=login]")
    print(usernameEle)
    usernameEle.send_keys("15723315609")
    passwordEle = webDriver.find_element(By.CSS_SELECTOR, "input[name=password]")
    print(passwordEle)
    passwordEle.send_keys("Czk1994#")
    btnsEle = webDriver.find_elements(By.CLASS_NAME, "v-btn__content")
    for btn in btnsEle:
        print(btn.text)
        if btn.text == "登录":
            btn.click()

    # 选择是
    noEle = webDriver.find_element(By.CLASS_NAME, "btn")
    print(noEle.text)
    noEle.click()
